<template>
	<view class="loginLayout">
		<view class="logo dis-centen">
			<image class="logo-icon" src="/static/logo.png" mode="aspectFill"></image>
			<view class="title">{{appName}} - 注册</view>
		</view>

		<view class="input-item">
			<input type="text" v-model="qq" placeholder="QQ号码" />
		</view>
		<view class="input-item">
			<input type="password" v-model="password" placeholder="请输入密码" />
		</view>
		<view class="input-item" v-if="needCode">
			<input type="text" v-model="code" placeholder="请输入邀请码" />
		</view>
		<view class="sub-box">
			<button class="sub-btn" @click="goReg()">注册</button>
			<view class="action">
				<text>找回密码</text>
				<navigator url="/pages/login/login">
					<text>返回登录</text>
				</navigator>
			</view>
		</view>
		<navigator url="/pages/link/link">
			<view class="link">
				联系客服
			</view>
		</navigator>
	</view>
</template>

<script setup>
	import {
		ref
	} from 'vue';
	import {
		apiDoReg,
		apiGetConfig
	} from '@/api/apis';
	import {
		onLoad
	} from "@dcloudio/uni-app"
	import {
		useAppStore
	} from '@/store/app';
	let appName = ref('用户注册')
	onLoad(() => {
		appName.value = useAppStore().$state.main.app_name
	})
	let qq = ref('')
	let password = ref('')
	let code = ref('')
	let needCode = ref(false)
	// 判断是否需要邀请码
	const getConfig = async () => {
		await apiGetConfig().then(res => {
			needCode.value = res.data['main']['need_inviteCode'] == 1 ? true : false
		})
	}
	getConfig()
	const goReg = async () => {
		if (qq.value == "" || password.value == "") {
			uni.showToast({
				title: "请将内容输入完整",
				icon: "none"
			})	
			return
		}
		if (needCode.value && code.value == "") {
			uni.showToast({
				title: "邀请码不能为空",
				icon: "none"
			})
			return
		}
		try {
			const res = await apiDoReg({
				qq: qq.value,
				password: password.value,
				invite_code: code.value
			});
			if (res.code === 200) {
				uni.showToast({
					title: '注册成功',
				})

			}
		} catch (err) {
			console.error("登录失败", err);
		}

	}
</script>

<style lang="scss" scoped>
	.loginLayout {
		display: flex;
		justify-content: center;
		align-items: center;
		position: relative;
		flex-direction: column;
		padding: 20rpx;
		position: absolute;
		top: 0%;
		bottom: 0;
		left: 0;
		right: 0;
		width: 100%;

		.title {
			margin: 30rpx 0;
			font-size: 45rpx;
			font-weight: 700;
		}

		.logo {
			flex-direction: column;

			.logo-icon,
			image {
				height: 130rpx;
				width: 130rpx;
				border-radius: 20rpx;
				margin-bottom: 10rpx;
			}
		}

		.input-item {
			width: 80%;
			margin-bottom: 30rpx;
			border-radius: 10rpx;
			background-color: #f5f5f5;

			input {
				height: 92rpx;
				padding-left: 20rpx;
				background-color: #f5f5f5;
			}

		}

		.sub-box {
			width: 80%;

			.sub-btn {
				width: 100%;
				background-color: #3176ff;
				color: #fff;
			}

			.action {
				width: 100%;
				display: flex;
				justify-content: space-between;
				align-items: center;
				height: 38rpx;
				margin-top: 20rpx;
				font-size: 28rpx;

				text {
					color: #797979;
				}
			}
		}

		.link {
			color: $theme-second-color;
			font-size: 28rpx;
			padding: 20rpx;
			margin-top: 20rpx;
		}

	}
</style>